﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Linq.Dynamic;
using SDT.Admin.Properties;
using SDT.Admin.ViewModels;
using SDT.Model;
using SDT.Model.Properties;
using SDT.Lib.Common;
using SDT.Lib.Paging;
using SDT.Lib.Helpers;
namespace SDT.Admin.Controllers
{
    [RoleHelper(Roles = RoleSystem.Dictionary)]
    public class ProductHolidayController : Controller
    {
        //
        // GET: /ProductHoliday/
        SDTDataContext eDataConttext = new SDTDataContext();
        public ActionResult Index(int? page)
        {
            try
            {
                if (page == null) page = 1;
                string order = UserState.getUserStateFromRequest("productHoliday.orderColum", "order", "ID").ToString();
                string orderDir = UserState.getUserStateFromRequest("productHoliday.orderDirection", "orderDir", "desc").ToString();
                string keyword = UserState.getUserStateFromRequest("productHoliday.Keyword", "Keyword", "").ToString();
                int pagesize = Convert.ToInt32(UserState.getUserStateFromRequest("productHoliday.PageSize", "PageSize", Settings.Default.PageSize));
                var listHolidays = eDataConttext.ProductHolidays
                    .Where(e => keyword.Trim().Length > 0 ? e.Name.ToLower().Contains(keyword.ToLower().Trim()) ||
                            e.ID.ToString()==keyword : true)
                    .OrderBy(string.Format("{0} {1}", order, orderDir))
                    .ToPagedList(page.Value - 1, pagesize);
                ViewData["order"] = order;
                ViewData["orderDir"] = orderDir;
                return View(new ProductHolidayViewModel
                {
                    ProductHolidays = listHolidays,
                    Keyword = keyword
                });
            }
            catch (Exception exp)
            {
                ViewData[Messages.MessageInfo] = exp.Message;
                return View();
            }
        }

        //
        // GET: /ProductHoliday/Details/5

        public ActionResult Details(int id)
        {
            var item = eDataConttext.ProductHolidays.Single(e => e.ID == id);
            return View(item);
        }

        //
        // GET: /ProductHoliday/Create

        public ActionResult Create()
        {
            ProductHoliday p = new ProductHoliday();
            return View(p);
        } 

        //
        // POST: /ProductHoliday/Create

        [HttpPost]
        public ActionResult Create(ProductHoliday p)
        {
            try
            {
                // TODO: Add insert logic here
                UpdateModel(p);
                eDataConttext.ProductHolidays.InsertOnSubmit(p);
                eDataConttext.SubmitChanges();
                return RedirectToAction("Index");
            }
            catch (Exception exp)
            {
                ViewData[Messages.MessageInfo] = exp.Message;
                return View();
            }
        }
        
        //
        // GET: /ProductHoliday/Edit/5
 
        public ActionResult Edit(int id)
        {
            var p = eDataConttext.ProductHolidays.Where(e=>e.ID==id).SingleOrDefault();
            return View(p);
        }

        //
        // POST: /ProductHoliday/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, ProductHoliday p)
        {
            try
            {
                // TODO: Add insert logic here
                p = eDataConttext.ProductHolidays.Where(e => e.ID == id).SingleOrDefault();
                UpdateModel(p);
                eDataConttext.SubmitChanges();
                return RedirectToAction("Index");
            }
            catch (Exception exp)
            {
                ViewData[Messages.MessageInfo] = exp.Message;
                return View();
            }
        }

        //
        // GET: /ProductHoliday/Delete/5
 
        public ActionResult Delete(int id)
        {
            try
            {
                var item = eDataConttext.ProductHolidays.Where(e => e.ID == id).SingleOrDefault();
                return View(item);
            }
            catch (Exception exp)
            {
                ViewData[Messages.MessageInfo] = exp.Message;
                return View();
            }
        }

        //
        // POST: /ProductHoliday/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                var item = eDataConttext.ProductHolidays.Single(e => e.ID == id);
                eDataConttext.ProductHolidays.DeleteOnSubmit(item);
                eDataConttext.SubmitChanges();
                return RedirectToAction("Index");
            }
            catch (Exception exp)
            {
                ViewData[Messages.MessageInfo] = exp.Message;
                return View();
            }
        }
    }
}
